program nllambda_spec2_co3

    version 13
	
	*Restrict and sort sample before entering here
    
    syntax varlist(min=97 max=97) [aw fw iw] if, at(name)
    local pref: word 1 of `varlist'
    local gr1977: word 2 of `varlist'
	local gr1978: word 3 of `varlist'
	local gr1979: word 4 of `varlist'
	local gr1980: word 5 of `varlist'
	local gr1981: word 6 of `varlist'
	local gr1982: word 7 of `varlist'
	local gr1983: word 8 of `varlist'
	local gr1984: word 9 of `varlist'
	local gr1985: word 10 of `varlist'
	local gr1986: word 11 of `varlist'
	local gr1987: word 12 of `varlist'
	local gr1988: word 13 of `varlist'
	local gr1989: word 14 of `varlist'
	local gr1990: word 15 of `varlist'
	local gr1991: word 16 of `varlist'
	local gr1992: word 17 of `varlist'
	local gr1993: word 18 of `varlist'
	local gr1994: word 19 of `varlist'
	local gr1995: word 20 of `varlist'
	local gr1996: word 21 of `varlist'
	local gr1997: word 22 of `varlist'
	local gr1998: word 23 of `varlist'
	local gr1999: word 24 of `varlist'
	local gr2000: word 25 of `varlist'
	local gr2001: word 26 of `varlist'
    local gr2002: word 27 of `varlist'
	local gr2003: word 28 of `varlist'
	local gr2004: word 29 of `varlist'
	local gr2005: word 30 of `varlist'
	local gr2006: word 31 of `varlist'
	local gr2007: word 32 of `varlist'
	local gr2008: word 33 of `varlist'
	local gr2009: word 34 of `varlist'
	local gr2010: word 35 of `varlist'
	local gr2011: word 36 of `varlist'
	local gr2012: word 37 of `varlist'
	local gr2013: word 38 of `varlist'
	local gr2014: word 39 of `varlist'
    local inflat: word 40 of `varlist'
    local ageit4: word 41 of `varlist'
	local ageit5: word 42 of `varlist'
local biryear: word 43 of `varlist'
local grindo1961: word 44 of `varlist'
local grindo1962: word 45 of `varlist' 
local grindo1963: word 46 of `varlist' 
local grindo1964: word 47 of `varlist' 
local grindo1965: word 48 of `varlist' 
local grindo1966: word 49 of `varlist' 
local grindo1967: word 50 of `varlist' 
local grindo1968: word 51 of `varlist' 
local grindo1969: word 52 of `varlist' 
local grindo1970: word 53 of `varlist' 
local grindo1971: word 54 of `varlist' 
local grindo1972: word 55 of `varlist' 
local grindo1973: word 56 of `varlist' 
local grindo1974: word 57 of `varlist' 
local grindo1975: word 58 of `varlist' 
local grindo1976: word 59 of `varlist' 
local grindo1977: word 60 of `varlist' 
local grindo1978: word 61 of `varlist' 
local grindo1979: word 62 of `varlist' 
local grindo1980: word 63 of `varlist' 
local grindo1981: word 64 of `varlist' 
local grindo1982: word 65 of `varlist' 
local grindo1983: word 66 of `varlist' 
local grindo1984: word 67 of `varlist' 
local grindo1985: word 68 of `varlist' 
local grindo1986: word 69 of `varlist' 
local grindo1987: word 70 of `varlist' 
local grindo1988: word 71 of `varlist' 
local grindo1989: word 72 of `varlist' 
local grindo1990: word 73 of `varlist' 
local grindo1991: word 74 of `varlist' 
local grindo1992: word 75 of `varlist' 
local grindo1993: word 76 of `varlist' 
local grindo1994: word 77 of `varlist' 
local grindo1995: word 78 of `varlist' 
local grindo1996: word 79 of `varlist' 
local grindo1997: word 80 of `varlist' 
local grindo1998: word 81 of `varlist' 
local grindo1999: word 82 of `varlist' 
local grindo2000: word 83 of `varlist' 
local grindo2001: word 84 of `varlist' 
local grindo2002: word 85 of `varlist' 
local grindo2003: word 86 of `varlist' 
local grindo2004: word 87 of `varlist' 
local grindo2005: word 88 of `varlist' 
local grindo2006: word 89 of `varlist' 
local grindo2007: word 90 of `varlist' 
local grindo2008: word 91 of `varlist' 
local grindo2009: word 92 of `varlist' 
local grindo2010: word 93 of `varlist' 
local grindo2011: word 94 of `varlist' 
local grindo2012: word 95 of `varlist' 
local grindo2013: word 96 of `varlist' 
local grindo2014: word 97 of `varlist' 

    tempname bcons bsd lam binf
    scalar `bcons' = `at'[1,1]
    scalar `bsd' = `at'[1,2]
    scalar `lam' = `at'[1,3]
	scalar `binf' = `at'[1,4]


	`if' {
	
	forval iflsi=4/5 {
	
	if `iflsi'==4 {
	local yrmax=2007
	}
	
	if `iflsi'==5 {
	local yrmax=2014
	}
	
	
	forval yr=1961/`yrmax' {
	
	
	
	
    tempvar weight_int`yr'_`iflsi'
	local k=`yrmax'-`yr'
	generate double `weight_int`yr'_`iflsi''=((`ageit`iflsi''-`k')/(`ageit`iflsi''))^`lam'
    replace `weight_int`yr'_`iflsi''=. if `k'>`ageit`iflsi'' | `biryear'<1961 | `biryear'>`yr'
	}
	
	
	local hola `weight_int1961_`iflsi''
	forval yr=1962/`yrmax'{
	local hola `hola' `weight_int`yr'_`iflsi''
	}
	
	tempvar weightsum`iflsi'
	egen `weightsum`iflsi''=rowtotal(`hola'), missing
	
	forval yr=1961/`yrmax'{
    tempvar weight`yr'_`iflsi'
	generate double `weight`yr'_`iflsi''=`weight_int`yr'_`iflsi''/`weightsum`iflsi''
	}
	

	
	forval yr=1961/`yrmax'{
	
	tempvar weightgrowth`yr'_`iflsi'
	generate double `weightgrowth`yr'_`iflsi''=.
	
	
	if `yr'>=1977 {
	
    replace `weightgrowth`yr'_`iflsi''=`weight`yr'_`iflsi''*`gr`yr'' if `yr'-`biryear'>15
	replace `weightgrowth`yr'_`iflsi''=`weight`yr'_`iflsi''*`grindo`yr'' if `yr'-`biryear'<=15
	
	local k=`yrmax'-`yr'
	replace  `weightgrowth`yr'_`iflsi''=. if `k'>`ageit`iflsi'' | `biryear'<1961 | `biryear'>`yr'
	}
	
	if `yr'<1977 {
	
	replace `weightgrowth`yr'_`iflsi''=`weight`yr'_`iflsi''*`grindo`yr'' if `yr'-`biryear'<=15
	
	local k=`yrmax'-`yr'
	replace  `weightgrowth`yr'_`iflsi''=. if `k'>`ageit`iflsi'' | `biryear'<1961 | `biryear'>`yr'
	}
	
	
	
	}
	
	local hola `weightgrowth1961_`iflsi''
	forval yr=1962/`yrmax'{
	local hola `hola' `weightgrowth`yr'_`iflsi''
	}
	

    tempvar gruse_`iflsi'
	egen `gruse_`iflsi''=rowtotal(`hola'), missing
	

	
	
	forval yr=1961/`yrmax'{

	
	tempvar eachnumSD`yr'_`iflsi'
	generate double `eachnumSD`yr'_`iflsi''=.
	
	if `yr'>=1977 {
	
	
    replace `eachnumSD`yr'_`iflsi''=`weight_int`yr'_`iflsi''*(`gr`yr''-`gruse_`iflsi'')^2 if `yr'-`biryear'>15
	 replace `eachnumSD`yr'_`iflsi''=`weight_int`yr'_`iflsi''*(`grindo`yr''-`gruse_`iflsi'')^2 if `yr'-`biryear'<=15
	
		local k=`yrmax'-`yr'
	
	replace `eachnumSD`yr'_`iflsi''=. if `k'>`ageit`iflsi'' | `biryear'<1961 | `biryear'>`yr'
	
	}
	
		if `yr'<1977 {
	
	
	 replace `eachnumSD`yr'_`iflsi''=`weight_int`yr'_`iflsi''*(`grindo`yr''-`gruse_`iflsi'')^2 if `yr'-`biryear'<=15
	
		local k=`yrmax'-`yr'
	
	replace `eachnumSD`yr'_`iflsi''=. if `k'>`ageit`iflsi'' | `biryear'<1961 | `biryear'>`yr'
	
	}
	
	
}


	local hola `eachnumSD1961_`iflsi''
	forval yr=1962/`yrmax'{
	local hola `hola' `eachnumSD`yr'_`iflsi''
	}

tempvar numtotalSD_`iflsi'
egen `numtotalSD_`iflsi''=rowtotal(`hola'), missing


tempvar sduse_`iflsi'
generate double `sduse_`iflsi''=sqrt((`ageit`iflsi''-1)*`numtotalSD_`iflsi''/((`ageit`iflsi''-2)*`weightsum`iflsi''))

	
	}

		
	
	tempvar sdusedif
	gen double `sdusedif' = `sduse_5' - `sduse_4'

	

    replace  `pref' = `bcons' + `bsd'*`sdusedif' +`binf'*`inflat' 

	
	}
	

	


end
